//
//  LayoutCellListItem.m
//  ebook
//
//  Created by 吴超飞 on 2017/3/16.
//  Copyright © 2017年 Moton. All rights reserved.
//

#import "LayoutCellListItem.h"
#import "UIView+Extension.h"

#define kDfFontSize 16.0 //字号
#define kFont(fontSize) [UIFont systemFontOfSize:fontSize]
#define kTextWidth(str, font) [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}].width

@interface LayoutCellListItem ()

@property (nonatomic, strong) UIImageView *bookCoverImv;//封面
@property (nonatomic, strong) UILabel *bookNameLabel;   //书名
@property (nonatomic, strong) UILabel *bookStatusLabel; //状态
@property (nonatomic, strong) UILabel *bookTypeLabel;   //类型
@property (nonatomic, strong) UILabel *bookLineLabel;   //分割线
@property (nonatomic, strong) UILabel *bookAuthorLabel; //作者
@property (nonatomic, strong) UILabel *introLabel;      //简介
@property (nonatomic, strong) UILabel *lineLabel;       //分割线

@property (nonatomic, assign) BOOL isShowCoverImage;//是否显示封面图片

@end

@implementation LayoutCellListItem

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier isShowCoverImage:(BOOL)isShow{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _isShowCoverImage = isShow;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    if(_isShowCoverImage)
        [self addBookCoverImv];
    
    [self addSubview:self.bookNameLabel];
    [self addSubview:self.bookStatusLabel];
    [self addSubview:self.bookTypeLabel];
    [self addSubview:self.bookLineLabel];
    [self addSubview:self.bookAuthorLabel];
    [self addSubview:self.introLabel];
    [self addSubview:self.lineLabel];
}

- (void)addBookCoverImv {
    
    float border = 2;
    UIImageView *backImv = [[UIImageView alloc] init];
    backImv.frame = CGRectMake(self.bookCoverImv.x-border, self.bookCoverImv.y-border, self.bookCoverImv.width+2*border, self.bookCoverImv.height+2*border);
    backImv.image = [UIImage imageNamed:@"bookcellBackground"];
   
    [self addSubview:backImv];
    [self addSubview:self.bookCoverImv];
}

-(UIImageView *)bookCoverImv{
    
    if (!_bookCoverImv) {
        
        UIImageView *coverImv = [[UIImageView alloc] init];
        coverImv.frame = CGRectMake(LayoutSpaceOut, 8, 70, 92);
        
        _bookCoverImv = coverImv;
    }
    return _bookCoverImv;
}

- (UILabel *)bookNameLabel {
   
    if (!_bookNameLabel) {
        
        float diff = _isShowCoverImage?CGRectGetWidth(self.bookCoverImv.frame):0;
        _bookNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10+diff+20, 10, kTextWidth(@"美人为馅", kDfFontSize), 20)];
        _bookNameLabel.font = kFont(kDfFontSize);
        _bookNameLabel.textColor = MTColor(51, 51, 51, 1.0);
        _bookNameLabel.text = @"修罗武神";
    }
    return _bookNameLabel;
}

- (UILabel *)bookStatusLabel {
  
    if (!_bookStatusLabel) {
        _bookStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.bookNameLabel.frame)+7, CGRectGetMinY(self.bookNameLabel.frame)+1, kTextWidth(@"完结", kDfFontSize)+5, 18)];
        _bookStatusLabel.font = kFont(kDfFontSize-2);
        _bookStatusLabel.textColor = MTColor(252, 252, 252, 1.0);
        _bookStatusLabel.backgroundColor = MTColor(255, 95, 83, 1.0);
        _bookStatusLabel.textAlignment = NSTextAlignmentCenter;
        _bookStatusLabel.layer.cornerRadius = 2;
        _bookStatusLabel.clipsToBounds = YES;
        _bookStatusLabel.text = @"连载";
    }
    return _bookStatusLabel;
}

- (UILabel *)bookTypeLabel {
   
    if (!_bookTypeLabel) {
        _bookTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookNameLabel.frame)+2, kTextWidth(@"都市言情", kDfFontSize-2), 18)];
        _bookTypeLabel.font = kFont(kDfFontSize-2);
        _bookTypeLabel.textColor = MTColor(161, 125, 41, 1.0);
        _bookTypeLabel.text = @"奇幻玄幻";
    }
    return _bookTypeLabel;
}

- (UILabel *)bookLineLabel {
    
    if (!_bookLineLabel) {
        _bookLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.bookTypeLabel.frame)+4, CGRectGetMinY(self.bookTypeLabel.frame)+3, 1, 18-4)];
        _bookLineLabel.backgroundColor = MTColor(153, 153, 153, 1.0);
    }
    return _bookLineLabel;
}

- (UILabel *)bookAuthorLabel {
   
    if (!_bookAuthorLabel) {
        _bookAuthorLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.bookLineLabel.frame)+3, CGRectGetMinY(self.bookTypeLabel.frame)+1, kTextWidth(@"善良的蜜蜂", kDfFontSize-2), 18)];
        _bookAuthorLabel.font = kFont(kDfFontSize-2);
        _bookAuthorLabel.textColor = [UIColor darkGrayColor];
        _bookAuthorLabel.text = @"善良的蜜蜂";
    }
    return _bookAuthorLabel;
}

- (UILabel *)introLabel {
  
    if (!_introLabel) {
        _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookTypeLabel.frame)+2, MTScreenW-12*2-CGRectGetMinX(self.bookNameLabel.frame), 92-(CGRectGetMaxY(self.bookTypeLabel.frame)-CGRectGetMinY(self.bookNameLabel.frame)))];
        _introLabel.font = kFont(kDfFontSize-3);
        _introLabel.textColor = [UIColor darkGrayColor];
        _introLabel.numberOfLines = 0;
        
        NSString *introduction = @"论潜力，不算天才，可玄功武技，皆可无视自动论实力，任凭你有万千至宝，但定不敌我界2月18日下午，天津市长王东峰主持召开市政府第85次常务会议，传达学习贯彻习近平总书记在省部级主要领导干部学习贯彻十八届六中全会精神专题研讨班上的重要讲话精神和市委常委会(扩大)会议精神，传达监察部《关于给予黄兴国开除处分的通知》《关于给予尹海林撤职处分的通知》。王东峰强调，要深入学习贯彻习近平总书记的重要讲话精神，旗帜鲜明讲政治，把牢固树立和自觉践行“四个意识”放在首位，认真落实《准则》《条例》，坚决拥护中央和监察部对黄兴国开除的决定、对尹海林撤职的决定，认真落实中央决策部署和市委要求，坚定不移推进政府系统全面从严治党和党风廉政建设及反腐败斗争向纵深发展，以优异成绩迎接党的十九大和市第十一次党代会胜利召开。";
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:introduction];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:2];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, introduction.length)];
        [attributedString addAttribute:NSFontAttributeName value:kFont(kDfFontSize-2) range:NSMakeRange(0, introduction.length)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, introduction.length)];
        
        _introLabel.attributedText = attributedString;
        _introLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
    }
    return _introLabel;
}

- (UILabel *)lineLabel {
   
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height-1, MTScreenW, 1)];
        _lineLabel.backgroundColor = MTColor(246, 246, 246, 1.0);
    }
    return _lineLabel;
}

- (void)setHidenUnderline:(BOOL)hidenUnderline {
   
    self.lineLabel.hidden = hidenUnderline;
}

-(void)setModel:(StoreBookModel *)model{
    
    [self.bookCoverImv sd_setImageWithURL:[NSURL URLWithString:model.Coverimg] placeholderImage:[UIImage imageNamed:@"bookcellBackground"]];
    
    //添加背景框
    float border = 2;
    UIImageView *backImv = [[UIImageView alloc] init];
    backImv.frame = CGRectMake(-border, -border, CGRectGetWidth(self.bookCoverImv.frame)+2*border, CGRectGetHeight(self.bookCoverImv.frame)+2*border);
    backImv.image = [UIImage imageNamed:@"bookcellBackground"];
    [self.bookCoverImv insertSubview:backImv atIndex:-999];
    
    self.bookNameLabel.text = model.Name;
    
    self.bookNameLabel.width = kTextWidth(self.bookNameLabel.text, kDfFontSize);
    self.bookStatusLabel.x = CGRectGetMaxX(self.bookNameLabel.frame)+7;
    if ([model.Fullflag intValue]==0) {
        self.bookStatusLabel.backgroundColor = MTColor(245, 92, 80, 1);
        self.bookStatusLabel.text = @"连载";
    } else {
        self.bookStatusLabel.backgroundColor = MTColor(84, 168, 144, 1);
        self.bookStatusLabel.text = @"完结";
    }
    
    self.bookAuthorLabel.text = model.Author;
    self.introLabel.text = model.Intro;
}

@end
